﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TLT.Models.Configuration
{
    public class TLTConvention : Convention
    {
        public TLTConvention()
        {
            // String Length(300)
            List<string> money = new List<string> { "DonGia", "GiaTriNT","GiaTriQD", "GiaBan","GiaGoc","GiaKhuyenMai" };

            // String Length(250)
            List<string> str250 = new List<string> { "TenVietTat", "TenTiengAnh", "BarCode", "Chat" };

            // String Length(50)
            List<string> str50 = new List<string> { "Email", "Website", "DiDong", "DienThoai", "MaSoThue", "CMTND", "GioiTinh", "SoTaiKhoan","Fax" };

            this.Properties()
                    .Where(p => p.Name == "Id")
                    .Configure(p => p.IsKey().HasColumnOrder(0).HasColumnOrder(0).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity));

            this.Properties()
                    .Where(p => p.Name == "STT")
                    .Configure(p => p.HasColumnOrder(1));

            // Chung
            this.Properties<String>()
                .Configure(c => c.HasMaxLength(300));

            this.Properties<DateTime?>()
                .Configure(c => c.HasColumnType("Date"));

            this.Properties()
                    .Where(p => str250.Contains(p.Name))
                    .Configure(p => p.HasMaxLength(250));

            this.Properties()
                    .Where(p => str50.Contains(p.Name))
                    .Configure(p => p.HasMaxLength(50));

            this.Properties()
                    .Where(p => p.Name == "Ma")
                    .Configure(p => p.HasMaxLength(50).HasColumnOrder(2));

            this.Properties()
                    .Where(p => p.Name == "Ten")
                    .Configure(p => p.HasMaxLength(250).HasColumnOrder(3));
            // Ảnh
            this.Properties<Byte[]>()
                .Configure(c => c.HasColumnType("image"));
            // Tiền tệ
            this.Properties()
                    .Where(p => money.Contains(p.Name))
                    .Configure(p => p.HasColumnType("Money"));            
        }
    }
}
